<template>
	<mescroll-body ref="mescrollRef" @init="mescrollInit" :up="upOption" :down="downOption" @up="upCallback" @down="downCallback">
		<view style="height: 20rpx;"></view>
		<view :class="['message',index===curretIndex?'infobg':'']" v-for="(item,index) in listdata" :key="index" @click="read_detail(index)">
			<view class="title">
				<view class="boxleft">
					<view :class="['point',item.is_read==0?'darkpint':'']"></view>
					<view :class="['subtitle',item.is_read==0 || index===curretIndex?'darktitle':'']">{{item.title}}</view>
				</view>
				<view class="time">{{item.created_at}}</view>
			</view>
			<view class="content" v-if="index===curretIndex">
				{{item.content}}
			</view>
		</view>
	</mescroll-body>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				userinfo: '',
				listdata: [],
				curretIndex: '',
				downOption: {
					autoShowLoading: true,
					textLoading: "玩命加载中...",
				},

				upOption: {
					noMoreSize: 4,
					empty: {
						tip: '~ 搜索无数据 ~',
						btnText: ''
					},
					textLoading: "玩命加载中...",
					textNoMore: "-- 人家是有底线的 --",
					auto: false
				},
				pagesize: 15
			}
		},
		onLoad() {
			this.userinfo = uni.getStorageSync('user_info')
			//打开本地数据库
			this.$sqliteDb.openSqlite()
		},
		onUnload() {
			this.$sqliteDb.closeSQL()
		},
		methods: {
			downCallback(e) {
				console.log(e)
				console.log('downCallback')
				this.getinitdata().then(res => {
					// console.log(res)
					//服务中返回数据
					if (res.length > 0) {
						console.log(111)
						res.map((item, index) => {
							let sql = "insert into message (`member_id`,`title`,`content`,`created_at`) values(" + this.userinfo.id +
								",'" + item.title + "','" + item.content + "','" + item.created_at + "')"
							this.$sqliteDb.executeSql(sql).then(ret => {
								console.log(ret);
							}).catch(error => {
								this.mescroll.endErr();
								console.log(error)
								this.$ShowMsg('error::'+JSON.stringify(error));
							})
						})
					}
					let sqlselect = "select * from message where `member_id`=" + this.userinfo.id + " order by `id` desc limit " +
						this.pagesize
					this.$sqliteDb.selectSql(sqlselect).then(sqlret => {
						console.log(sqlret)
						this.listdata = sqlret
						this.mescroll.setPageNum(2) //重置第2页开始下拉 
						this.mescroll.endSuccess(sqlret.length)
					}).catch(error => {
						//联网失败, 结束加载
						this.mescroll.endErr();
						console.log(error)
						this.$ShowMsg('error::'+JSON.stringify(error));
					})
				})
			},
			upCallback(page) {
				console.log('upCallback')
				console.log(page.num);
				if (page.num == 1) this.listdata = []
				let sql = "select * from message where `member_id`=" + this.userinfo.id + " order by `id` desc limit " + (page.num -
						1) *
					this.pagesize + "," + this.pagesize
				this.$sqliteDb.selectSql(sql).then(ret => {
					console.log(ret.length)
					this.listdata = this.listdata.concat(ret); //追加新数据
					this.mescroll.endSuccess(ret.length)
				})
			},
			getinitdata() {
				return new Promise((resolve, reject) => {
					this.$HttRequest({
						url: '/message/list',
						param: {
							is_update: 'true'
						}
					}).then((res) => {
						console.log(res)
						if (res.status === 10000) {
							resolve(res.data);
						} else {
							this.$ShowMsg(res.message);

						}
					}).catch((error) => {
						//异常错误
						reject(error.errMsg);
					});
				});

			},

			read_detail(index) {
				this.curretIndex = index
				if (this.listdata[index].is_read == 0) {
					let sql = "update message set `is_read`=1 where `id`=" + this.listdata[index].id
					this.$sqliteDb.executeSql(sql).then(ret => {
						this.listdata[index].is_read = true
						if (this.$common.state.messagecount > 0) {
							this.$common.state.messagecount -= 1
							if (this.$common.state.messagecount == 0) {
								uni.hideTabBarRedDot({
									index: 3
								})
							}
						}
					}).catch(e => {
						console.log(e)
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.message {
		width: 90%;
		margin: 0 auto;
		padding: 20rpx 0;
		background-color: #fff;
		border-bottom: 1rpx solid #e7e7eb;
		display: flex;
		justify-content: center;
		align-items: flex-start;
		flex-direction: column;
		font-size: 28rpx;


		.content {

			margin-top: 20rpx;
			padding: 10rpx 40rpx;
			color: #474747;

			line-height: 40rpx;
		}

		.title {

			width: 100%;
			display: flex;
			justify-content: center;
			align-items: flex-start;
			flex-direction: column;
			padding-left: 30rpx;

			.boxleft {
				display: flex;
				justify-content: flex-start;
				align-items: center;

				.point {
					width: 10rpx;
					height: 10rpx;
					margin: 0 10rpx;
					border-radius: 50%;
					background-color: #d5d5d5;
				}

				.darkpint {
					background-color: #FF0000;
				}

				.subtitle {
					color: #9a9a9a;
				}

				.darktitle {
					color: #474747;
				}
			}

			.time {
				color: #cdcdcd;
				margin-top: 10rpx;
				margin-left: 30rpx;
				font-size: 22rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}

	.infobg {
		background-color: #f4f5f9;
	}
</style>
